import java.util.Vector;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordListener;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotFoundException;

public class ApplicationSettings implements RecordListener {
	
	private static final String FROM = "FROM";
	private static final String PASSWORD = "PASSWORD";
	private static final String USERNAME = "USERNAME";
	private static final String API_ID = "API_ID";
	// Name of record store of application.
	private final String RECORD_STORE_NAME = "ClickSmsStore";
	// Record store of application.
	private RecordStore recordStore;
	// Array of settings deserialized from records in record store.
	private Vector appSettings;

	/**
	 * Constructor
	 */
	public ApplicationSettings() throws RecordStoreException {
		initializeComponents();
	}

	/**
	 * Initializes components
	 */
	private void initializeComponents() throws RecordStoreException,
			SecurityException {
		// Create array of app settings.
		appSettings = new Vector();

		// Open record store, read settings from it and display settings.
		openRecordStore();
		getAppSettings();
	}

	/**
	 * Opens named record store of application. Record store will be created if
	 * it is not exists.
	 */
	private void openRecordStore() throws RecordStoreException,
			SecurityException {
		recordStore = RecordStore.openRecordStore(RECORD_STORE_NAME, true);
		recordStore.addRecordListener(this);
	}

	/**
	 * Closes record store.
	 */
	private void closeRecordStore() {
		try {
			recordStore.closeRecordStore();
		} catch (RecordStoreException ex) {
			// Do nothing.
		}
	}

	/**
	 * Reads settings from record store and adds it to array of settings.
	 * 
	 * @throws RecordStoreException
	 *             if error occurs on working with record store.
	 */
	private void getAppSettings() throws RecordStoreException {
		appSettings.removeAllElements();

		RecordEnumeration recEnum = recordStore.enumerateRecords(null, null,
				false);
		while (recEnum.hasNextElement() == true) {
			int settingId = recEnum.nextRecordId();
			byte[] data = recordStore.getRecord(settingId);
			AppSetting setting = AppSetting.deserialize(data);
			if (setting != null) {
				appSettings.addElement(setting);
			}
		}
		recEnum.destroy();
	}

	/**
	 * Adds settings to settings list control from array of settings.
	 */
	private String getSetting(String name) {		
		String result = "";
		for (int index = 0; index < appSettings.size(); ++index) {
			AppSetting setting = (AppSetting) appSettings.elementAt(index);
			if (setting.getName().equals(name)) {
				result = setting.getValue();
			}
		}		
		return result;
	}

	/**
	 * Adds setting to record store.
	 * 
	 * @param name
	 *            - name of new record.
	 * @param value
	 *            - value of new record.
	 */
	public void addSetting(String name, String value)
			throws RecordStoreException {

		AppSetting setting = new AppSetting(name, value);
		byte[] data = setting.serialize();
		recordStore.addRecord(data, 0, data.length);
		getAppSettings();
	}

	/**
	 * Deletes record store and creates it again.
	 * 
	 * @throws RecordStoreException
	 * @throws RecordStoreNotFoundException
	 */
	public void deleteAllSettings() throws RecordStoreException {
		recordStore.closeRecordStore();
		RecordStore.deleteRecordStore(RECORD_STORE_NAME);
		openRecordStore();
	}
	
	public String getFrom() {
		return getSetting(FROM);
	}

	public String getApiID() {
		return getSetting(API_ID);
	}
	
	public String getUsername() {
		return getSetting(USERNAME);
	}
	
	public String getPassword() {
		return getSetting(PASSWORD);
	}
	
	public void updateSettings(String from, String apiId, String username, String password)  throws RecordStoreException {
		deleteAllSettings();
		addSetting(FROM, from);
		addSetting(API_ID, apiId);
		addSetting(USERNAME, username);
		addSetting(PASSWORD, password);
	}
	
	/**
	 * From MIDlet. Signals the MIDlet to terminate and enter the Destroyed
	 * state.
	 */
	public void destroy() {
		closeRecordStore();
	}

	/**
	 * From RecordListener. Called after a record in a record store has been
	 * changed.
	 * 
	 * @param store
	 *            - the RecordStore in which the record is stored
	 * @param recordId
	 *            - the recordId of the record that has been added
	 */
	public void recordChanged(RecordStore store, int recordId) {
		// No implementation required.
	}

	/**
	 * From RecordListener. Called after a record has been deleted from a record
	 * store.
	 * 
	 * @param store
	 *            - the RecordStore in which the record is stored
	 * @param recordId
	 *            - the recordId of the record that has been added
	 */
	public void recordDeleted(RecordStore store, int recordId) {
		// No implementation required.
	}

	public void recordAdded(RecordStore arg0, int arg1) {
		// TODO Auto-generated method stub

	}
}